<?php

namespace app\common\model;

use app\common\model\trait\ConstIsPass;

/**
 * 实名认证模型
 * 2024年3月29日22:15:20
 */
class Authentication extends base\Authentication
{
    use ConstIsPass;

    protected $append = ['attatch_json', 'exert_type_text', 'farm_type_text', "is_pass_text"];
    /**
     * 农民类型
     */
    const TYPE_FARMER = 1;
    /**
     * 专家类型
     */
    const TYPE_EXPERT = 2;

    /**
     * 普通专家
     */
    const EXPERT_TYPE_NORMAL = 0;
    /**
     * 科技特派员
     */
    const EXPERT_TYPE_WORKER = 1;
    /**
     * 普通农户
     */
    const FARMER_TYPE_NORMAL = 0;
    /**
     *驻镇队长
     */
    const FARMER_TYPE_WORKER = 1;

    /**
     * 通过
     * @deprecated
     */
    const IS_PASS_PASSED = 1;



    public function getAreaFullNameAttr()
    {
        $text = "";
        $area = AreaCode::where("code", $this->area_code)->find();
        if ($area) {
            if ($area->parent) {
                $text .= $area->parent->getAttr("name");
            }

            $text .= " " . $area->getAttr("name");

        }
        return $text;

    }

    public function user(): \think\model\relation\HasOne
    {
        return $this->hasOne(User::class, "id", "user_id");
    }


    public function areaCodeModel(): \think\model\relation\HasOne
    {
        return $this->hasOne(AreaCode::class, "code", "area_code")->hidden(["coordinates"]);
    }

    public function getExertTypeTextAttr(): string
    {
        $model = $this;
        $type = $model->getAttr("type");
        $typeText = "";
        if ($type == self::TYPE_EXPERT) {
            if ($model->expert_type == self::EXPERT_TYPE_NORMAL) {
                $typeText = "普通专家";
            }
            if ($model->expert_type == self::EXPERT_TYPE_WORKER) {
                $typeText = "科技特派员";
            }
        }
        return $typeText;
    }

    public function getFarmTypeTextAttr(): string
    {
        $model = $this;
        $type = $model->getAttr("type");
        $typeText = "";
        if ($type == self::TYPE_FARMER) {
            if ($model->farmer_type == self::FARMER_TYPE_NORMAL) {
                $typeText = "需求方";
            }
            if ($model->farmer_type == self::FARMER_TYPE_WORKER) {
                $typeText = "驻镇队长";
            }
        }
        return $typeText;
    }


    public function getAttatchJsonAttr()
    {
        if ($this->attatch) {
            return json_decode($this->attatch, true);
        }
        return [];

    }

    /**
     * 根据area补充area_code
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function autoFixedAreaCode(): void
    {
        $model = $this;
//        var_dump("===");
//        var_dump($model->area_code);
        if (!$model->area_code) {
            $where = [];
            $where[] = ["level", "=", 3];//只找县
            $areaCodeList = AreaCode::where($where)->select();
            foreach ($areaCodeList as $key => $value) {
                //在area字符串中是否包含
//                var_dump($model->area);
//                var_dump($value->getAttr("name"));
                if (str_contains($model->area, $value->getAttr("name"))) {
                    $model->area_code = $value->code;
//                    var_dump($model->area_code);
//                    var_dump("---");
                    break;
                }
            }
        }
    }

}